package ee.az.test.geometry.block;

import org.apache.commons.lang.StringUtils;

/**
 * @author Artur Zinatullin (artur.zinatullin@gmail.com) 2010
 */
public class Field {
	private final int width;
	private final int height;
	private final int field[][]; // array of rows, every row is array of int
	
	public Field(int[][] field) {
		this.width = field[0].length;
		this.height = field.length;
		this.field = new int[this.height][this.width];
		for (int x = 0; x < this.width; x++) {
			for (int y = 0; y < this.height; y++) {
				setValue(x, y, field[y][x]);
			}
		}
	}

	public Field(int width, int height) {
		this.width = width;
		this.height = height;
		this.field = new int[this.height][this.width];
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();

		int width = 0;
		for (int y = 0; y < getHeight(); y++) {
			for (int x = 0; x < getWidth(); x++) {
				width = Math.max(width, String.valueOf(getValue(x, y)).length());
			}
		}

		for (int y = 0; y < getHeight(); y++) {
			sb.append("[");
			for (int x = 0; x < getWidth(); x++) {
				sb.append(StringUtils.leftPad(String.valueOf(getValue(x, y)), width+1));
			}
			sb.append(" ]\n");
		}
		return sb.toString();
	}
	
	public int getValue(int x, int y) {
		return field[y][x];
	}
	public int setValue(int x, int y, int value) {
		int oldValue = getValue(x, y);
		field[y][x] = value;
		return oldValue;
	}
	
	public int getWidth() {
		return width;
	}
	public int getHeight() {
		return height;
	}
}
